import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login'
// import Home from '../components/Home'
// import Welcome from '../components/Welcome'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome')
// import Users from '../components/user/Users'
// import Rights from '../components/power/Rights'
// import Roles from '../components/power/Roles'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles')
// import Cate from '../components/goods/Cate'
// import Params from '../components/goods/Params'
const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params')
// import List from '../components/goods/List'
// import Add from '../components/goods/Add'
const List = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/List')
const Add = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Add')
// import Order from '../components/order/Order'
// import Reports from '../components/report/Reports'
const Order = () => import(/* webpackChunkName: "Order_Reports" */ '../components/order/Order')
const Reports = () => import(/* webpackChunkName: "Order_Reports" */ '../components/report/Reports')
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Reports }
      ]
    }
  ]
})
// 挂载一个beforeEach路由导航守卫
// to 将要访问的路径
// from 从哪个路径跳转而来
// next 是一个函数表示放行
// next() 放行 next('/login') 强制跳转
router.beforeEach((to, from, next) => {
  // 如果用户访问的是登录页直接放行
  if (to.path === '/login') return next()
  // 从sessionStorage中获取保存到的token值
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果没有token值，则强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})

export default router
